﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using PhotgalleryEntities.Interfaces;
using PhotoGalleryEnteties;
using PhotogalleryModel;
using photogalleryMVC.Models;

namespace photogalleryMVC.Controllers
{
    public class AlbumController : PhotoGalleryController 
    {


        private const string  PagingCookieName="AlbumItemsOnPage" ;

        [AcceptVerbs( HttpVerbs.Post )]
        public ActionResult ItemsOnPage(int? onPage, string returnUrl)
        {
            HttpContext .SetPaginationCookie(PagingCookieName, onPage);
            if (string.IsNullOrEmpty(returnUrl))
                return RedirectToAction("Index", "Home");
            return Redirect(returnUrl);
        }



        //
        // GET: /Album/Details/5
        // GET: /Album/Details/5/Page/2

        
  
        public ActionResult Details(int id,int? page)
        {

            int? itemsOnPage;
            int totalPages;
            SelectList quantityList = Request.GetItemsOnPage(PagingCookieName, out itemsOnPage);           
            Album album = AlbumRepository.GetAlbumById(id);
            ItemsPage<IAlbumItem> items = AlbumRepository.GetPage(id, itemsOnPage, page ?? 1, out totalPages);


            AlbumDetailModel model = new AlbumDetailModel()
                                         {
                                             OwnerName = album.Owner.UserName,
                                             ParentAlbums = new Dictionary<int, string>(),
                                             Title = album.Title,
                                             AlbumId = album.AlbumId,
                                             Content = items.Page,
                                             CurrentPage = items.PageNumber,
                                             TotalPages = totalPages,
                                             QuantityList = quantityList
                                             
                                         } ;
            
            //populating data for partial views 
            ViewData["IsEditor"] = String.Compare(User.Identity.Name, album.Owner.UserName, true) == 0 ||
                                   User.IsInRole("admin");
            ViewData["CurrentPage"] = items.PageNumber ;
            ViewData["AlbumId"] = album.AlbumId;
            //populating hierarchy section
            Album parentAlbum = album.ParentAlbum;
            while(parentAlbum!=null)
            {
                model.ParentAlbums.Add(parentAlbum.AlbumId, parentAlbum.Title);
                parentAlbum = parentAlbum.ParentAlbum;
            }

            if (Request.IsAjaxRequest())
                return View("AlbumItemsPartialView", model);
            return View(model);
        }

        //
        // GET: /CreateAlbumIn/Id

        [Authorize]
        [AccessToResource( "parentAlbumId",ResourceType.Album )]
        public ActionResult Create(int parentAlbumId)
        {

            if (Request.IsAjaxRequest())
                return View("CreatePartial");
            return View();
        } 

        //
        // POST:/CreateAlbumIn/Id


        [Authorize]
        [AccessToResource("parentAlbumId",ResourceType.Album )]
        [HttpPost]
        public ActionResult Create(FormCollection collection, int parentAlbumId)
        {
            Album parentAlbum = AlbumRepository.GetAlbumById(parentAlbumId );           
            Album newAlbum = AlbumRepository.CreateAlbum();
            newAlbum.ParentAlbum = parentAlbum;
            newAlbum.Owner = parentAlbum.Owner;
            
            UpdateModel(newAlbum);
            AlbumRepository.ValidateAlbum(newAlbum).RegisterErrors(ModelState);
            if(ModelState.IsValid )
            {
                ModelContext.SaveChanges();
                 return RedirectToAction("Details", new {id = newAlbum.AlbumId});
            }
            return View();

        }


        //
        // GET: /Album/Delete/5


 
        [Authorize ]
        [AccessToResource("id",ResourceType.Album  )]
        public ActionResult Delete(int id)
        {
            Album album = AlbumRepository.GetAlbumById(id);         
            int parentAlbumId = album.ParentAlbum.AlbumId;
            AlbumRepository.DeleteAlbum(album);
            ModelContext .SaveChanges();
            return RedirectToAction("Details", new {id = parentAlbumId});
        }   
        
    }

    



  
}
